<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>汇率信息设置</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<%@include file="common.jsp"%>
</head>
<body style="margin-top:-10px;">
	<div style="margin-top:20px;">
		<button onclick="addBankExchangeRate()" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#addBankExchangeRateBox">新增汇率信息</button>
		<button onclick="delbankExchangeRate()" class="btn btn-primary btn-lg" data-toggle="modal">删除汇率信息</button>
	</div>
	<table id="bankExchangeRateTable" class="text-nowrap"></table>
	<div class="modal fade" id="addBankExchangeRateBox" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content" style="width:400px;">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">添加汇率信息</h4>
                <h4 class="modal-title" id="myModalLabel2">修改汇率信息</h4>
            </div>
            <div class="modal-body">
           		 <form id="bankExchangeRateForm" method='post' class="" enctype="multipart/form-data">
           		 	<input id="bankExchangeRateId" name="id" hidden="true"/>
				 	<div class="form-group" style="margin-left:10px;">
				    	<label for="exampleInputName2">银行信息：</label>
				    	<select id="bankCode" name="bankCode" class="form-control" style="width:179px;padding: 0 10px 0 0;"></select>
				  	</div>
				  	<div class="form-group" style="margin-left:10px;margin-top:20px;">
				    	<label for="exampleInputEmail2">用户类别：</label>
				    	<select id="userTypeCode" name="userTypeCode" class="form-control" style="width:179px;padding: 0 10px 0 0;"></select>
				  	</div>
				  	<div class="form-group" style="margin-left:10px;margin-top:20px;">
				  		<label for="exampleInputEmail2">兑换汇率：</label>
				  		<input id="bankExchangeRate" name="bankExchangeRate" type="number" style="width:179px" class="form-control"/>
				  	</div>
				</form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button id="add" type="button" class="btn btn-primary" onclick="submitBankExchangeRate()">提交</button>
                <button id="upd" type="button" class="btn btn-primary" onclick="updateBankExchangeRate()">修改</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
	</div>
</body>
<script type="text/javascript">
//重载表格
$(function(){
	//加载表格
	$("#bankExchangeRateTable").bootstrapTable({
		url: "${pageContext.request.contextPath}/bankExchangeRate/findBankExchangeRate.do",
		method: "post",
		cache: false,
		dataType: "json",
		contentType: "application/x-www-form-urlencoded",
		toolbar: "#toolbar",
		classes: "table table-hover table-striped",
		height:450,
		pagination: true,
		sidePagination: 'server',
		search: false,
		smartDisplay:false,
		sortOrder: "desc",
		searchText: "",
		showRefresh: false,
		showToggle: false,
		singleSelect: true,
		showPaginationSwitch: false,
		pageNumber: 1,
		pageSize: 5,
		pageList: [5, 10, 20, 40],
		clickToSelect: true,
		onCheck: function (row) {
		},
		queryParamsType: "undefined",
		queryParams: function (params) {
			var params = {
				pageNum: params.pageNumber,
				pageSize: params.pageSize,
				sortBy: params.sortOrder,
			}
			return params;
		},
		columns: [
			{ checkbox: true }
			,{
				field: "bankName",
				title: "银行名称"
			}, {
				field: "userTypeName",
				title: "用户类别"
			}, {
				field: "bankExchangeRate",
				title: "汇率"
			}, {
				field: "deleteFlag",
				title: "删除标识"
			}, {
				field: "createTime",
				title: "创建时间",
				formatter: formatTime
			}
			
		],
		responseHandler: function (data) {//onLoad前处理数据源
			return data;
		},
		onLoadSuccess: function (data) {	//成功时执行
		},
		onLoadError: function (data) {	//失败时执行
			return;
		},
		//双击行事件
		onDblClickRow:function(row){
			//弹出框标题
			$('#myModalLabel').hide();
			$('#myModalLabel2').show();
			//按钮
			$('#add').hide();
			$('#upd').show();
			
			//控件赋值
			$('#bankExchangeRate').val(row.bankExchangeRate);
			$('#bankCode').val(row.bankCode);
			$('#userTypeCode').val(row.userTypeCode);
			$('#bankExchangeRateId').val(row.id);
			//打开弹出框
			$('#addBankExchangeRateBox').modal('show');
		}
	});
	//重载表格
	$(window).resize(function () {
		$('#bankExchangeRateTable').bootstrapTable('resetView');
	});
	//加载银行信息下拉框
	$.ajax({
		url:"${pageContext.request.contextPath}/bank/findBank.do",
		contentType:"application/x-www-form-urlencoded",
		//data:submitData,
		type:"post",
		cache:false,//false是不缓存，true为缓存
	    async:true,//true为异步，false为同步
		success:function(data){
			var datas = jQuery.parseJSON(data);
			var bankCode = $("#bankCode");
			for (var i = 0; i < datas.rows.length; i++) {
				//获取上级类别
				bankCode.append("<option value='"+datas.rows[i].bankCode+"'>"
						+ datas.rows[i].bankName + "</option>");
			}
		}
	});
	//加载用户类别下拉框
	$.ajax({
		url:"${pageContext.request.contextPath}/relation/loadUserType.do",
		contentType:"application/x-www-form-urlencoded",
		//data:submitData,
		type:"post",
		cache:false,//false是不缓存，true为缓存
	    async:true,//true为异步，false为同步
		success:function(data){
			var datas = jQuery.parseJSON(data);
			var userTypeCode = $("#userTypeCode");
			for (var i = 0; i < datas.length; i++) {
				//获取上级类别
				userTypeCode.append("<option value='"+datas[i].userTypeCode+"'>"
						+ datas[i].userTypeName + "</option>");
			}
		}
	})
})
//新增银行信息点击事件
function addBankExchangeRate(){
	//弹出框标题
	$('#myModalLabel').show();
	$('#myModalLabel2').hide();
	//按钮
	$('#add').show();
	$('#upd').hide();
	//清空表单
	document.getElementById("bankExchangeRateForm").reset()
}
//新增银行汇率信息
function submitBankExchangeRate(){
	if($('#bankExchangeRate').val() == ''){
		return alert("请输入兑换汇率！");
	}
	//只传递产品编码做为查询条件
	var datas = "bankCode="+$('#bankCode').val()+"&userTypeCode="+$('#userTypeCode').val();
	//银行编码数据唯一性校验
	$.ajax({
		url:"${pageContext.request.contextPath}/bankExchangeRate/findBankExchangeRate.do",
		contentType:"application/x-www-form-urlencoded",
		//只把银行编码传递到后台
		data: datas,
		type:"post",
		processData: false,
		cache:false,//false是不缓存，true为缓存
	    async:true,//true为异步，false为同步
		success:function(data){
			var datas = jQuery.parseJSON(data);
			if(datas.rows.length > 0){
				return alert('汇率存在！');
			}else{
				//提交表单，新增银行信息
				$('#bankExchangeRateForm').attr("action","${pageContext.request.contextPath}/bankExchangeRate/addBankExchangeRate.do");
				$('#bankExchangeRateForm').submit();
				alert('新增成功！');
			}
		}
	})
}
//删除银行汇率信息
function delbankExchangeRate(){
	var rows = $('#bankExchangeRateTable').bootstrapTable('getSelections');
	if(rows.length <= 0){
		return alert("请选择数据！");
	}
	if(confirm("确认删除？")){
		//from对象序列化
		//var data = $('#bankForm').serialize();
		//var submitData = decodeURIComponent(data,true);
		var row = rows[0];
		$.ajax({
			url:"${pageContext.request.contextPath}/bankExchangeRate/delbankExchangeRate.do",
			data: {
				id:row.id
			},
			type:"post",
			dataType:"json",
			processData: true,
			success:function(data){
				alert(data);
				//刷新表格数据
				$('#bankExchangeRateTable').bootstrapTable('refresh');
			}
		})
	}
}
/**
 * 修改汇率信息
 */
function updateBankExchangeRate(){
	if(confirm("确认修改？")){
		//from对象序列化
		//var data = $('#bankTypeForm').serialize();
		//var submitData = decodeURIComponent(data,true);
		var submitData = new FormData(document.getElementById("bankExchangeRateForm"));
		$.ajax({
			url: "${pageContext.request.contextPath}/bankExchangeRate/updateBankExchangeRate.do",
			data: submitData,
			type: "post",
			processData: false,
			contentType:false,
			success:function(data){
				alert(data);
				//刷新表格数据
				$('#bankExchangeRateTable').bootstrapTable('refresh');
				//隐藏模态框
				$('#addBankExchangeRateBox').modal('hide');
			}
		})
	}
}
</script>
</html>




